package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type ProgramInfoService struct {
}

// CreateProgramInfo 创建ProgramInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programInfoService *ProgramInfoService) CreateProgramInfo(programInfo autocode.ProgramInfo) (err error) {
	err = global.GVA_DB.Create(&programInfo).Error
	return err
}

// DeleteProgramInfo 删除ProgramInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programInfoService *ProgramInfoService)DeleteProgramInfo(programInfo autocode.ProgramInfo) (err error) {
	err = global.GVA_DB.Delete(&programInfo).Error
	return err
}

// DeleteProgramInfoByIds 批量删除ProgramInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programInfoService *ProgramInfoService)DeleteProgramInfoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.ProgramInfo{},"id in ?",ids.Ids).Error
	return err
}

// UpdateProgramInfo 更新ProgramInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programInfoService *ProgramInfoService)UpdateProgramInfo(programInfo autocode.ProgramInfo) (err error) {
	err = global.GVA_DB.Save(&programInfo).Error
	return err
}

// GetProgramInfo 根据id获取ProgramInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programInfoService *ProgramInfoService)GetProgramInfo(id uint) (err error, programInfo autocode.ProgramInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&programInfo).Error
	return
}

// GetProgramInfoInfoList 分页获取ProgramInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programInfoService *ProgramInfoService)GetProgramInfoInfoList(info autoCodeReq.ProgramInfoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&autocode.ProgramInfo{}).Order("is_delete, sour, created_at desc")
    var programInfos []autocode.ProgramInfo
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
	err = db.Limit(limit).Offset(offset).Find(&programInfos).Error
	return err, programInfos, total
}
